from typing import TYPE_CHECKING

from .csv import CSVLogger
from .sqlite import SQLLogger
from .text import BaseTextLogger
from .xlsx import XLSXLogger

if TYPE_CHECKING:
    from src.config import Parameter

__all__ = ["RecordManager"]


class RecordManager:
    """检查数据储存路径和文件夹"""
    works_keys = (
        "type",
        "collection_time",
        "uid",
        "sec_uid",
        "unique_id",
        "short_id",
        "id",
        "desc",
        "text_extra",
        "duration",
        "ratio",
        "height",
        "width",
        "share_url",
        "create_time",
        "uri",
        "nickname",
        "user_age",
        "signature",
        "downloads",
        "music_author",
        "music_title",
        "music_url",
        "origin_cover",
        "dynamic_cover",
        "tag_1",
        "tag_2",
        "tag_3",
        "digg_count",
        "comment_count",
        "collect_count",
        "share_count",
        "extra",
    )
    works_text = (
        "作品类型",
        "采集时间",
        "UID",
        "SEC_UID",
        "抖音号",
        "SHORT_ID",
        "作品ID",
        "作品描述",
        "作品话题",
        "视频时长",
        "视频分辨率",
        "视频高度",
        "视频宽度",
        "作品链接",
        "发布时间",
        "视频URI",
        "账号昵称",
        "年龄",
        "账号签名",
        "下载地址",
        "音乐作者",
        "音乐标题",
        "音乐链接",
        "静态封面",
        "动态封面",
        "标签_1",
        "标签_2",
        "标签_3",
        "点赞数量",
        "评论数量",
        "收藏数量",
        "分享数量",
        "额外信息"
    )
    works_type = (
        "TEXT",
        "TEXT",
        "TEXT",
        "TEXT",
        "TEXT",
        "TEXT",
        "TEXT",
        "TEXT",
        "TEXT",
        "TEXT",
        "TEXT",
        "INTEGER",
        "INTEGER",
        "TEXT",
        "TEXT",
        "TEXT",
        "TEXT",
        "INTEGER",
        "TEXT",
        "TEXT",
        "TEXT",
        "TEXT",
        "TEXT",
        "TEXT",
        "TEXT",
        "TEXT",
        "TEXT",
        "TEXT",
        "INTEGER",
        "INTEGER",
        "INTEGER",
        "INTEGER",
        "TEXT",
    )
    comment_keys = (
        "collection_time",
        "cid",
        "create_time",
        "uid",
        "sec_uid",
        "short_id",
        "unique_id",
        "nickname",
        "signature",
        "user_age",
        "ip_label",
        "text",
        "sticker",
        "image",
        "digg_count",
        "reply_comment_total",
        "reply_id",
        "reply_to_reply_id",
    )
    comment_title = (
        "采集时间",
        "评论ID",
        "评论时间",
        "UID",
        "SEC_UID",
        "SHORT_ID",
        "抖音号",
        "账号昵称",
        "账号签名",
        "年龄",
        "IP归属地",
        "评论内容",
        "评论表情",
        "评论图片",
        "点赞数量",
        "回复数量",
        "回复ID",
        "回复对象",
    )
    comment_type = (
        "TEXT",
        "TEXT",
        "TEXT",
        "TEXT",
        "TEXT",
        "TEXT",
        "TEXT",
        "TEXT",
        "TEXT",
        "INTEGER",
        "TEXT",
        "TEXT",
        "TEXT",
        "TEXT",
        "INTEGER",
        "INTEGER",
        "TEXT",
        "TEXT",
    )
    user_keys = (
        "collection_time",
        "nickname",
        "url",
        "signature",
        "unique_id",
        "user_age",
        "gender",
        "country",
        "province",
        "city",
        "district",
        "ip_location",
        "verify",
        "enterprise",
        "sec_uid",
        "uid",
        "short_id",
        "avatar",
        "cover",
        "aweme_count",
        "total_favorited",
        "favoriting_count",
        "follower_count",
        "following_count",
        "max_follower_count",
    )
    user_title = (
        "采集时间",
        "昵称昵称",
        "账号链接",
        "账号签名",
        "抖音号",
        "年龄",
        "性别",
        "国家",
        "省份",
        "城市",
        "地区",
        "IP归属地",
        "标签",
        "企业",
        "SEC_UID",
        "UID",
        "SHORT_ID",
        "头像链接",
        "背景图链接",
        "作品数量",
        "获赞数量",
        "喜欢数量",
        "粉丝数量",
        "关注数量",
        "粉丝最大值",
    )
    user_type = (
        "TEXT",
        "TEXT",
        "TEXT",
        "TEXT",
        "TEXT",
        "INTEGER",
        "TEXT",
        "TEXT",
        "TEXT",
        "TEXT",
        "TEXT",
        "TEXT",
        "TEXT",
        "TEXT",
        "TEXT",
        "TEXT",
        "TEXT",
        "TEXT",
        "TEXT",
        "INTEGER",
        "INTEGER",
        "INTEGER",
        "INTEGER",
        "INTEGER",
        "INTEGER",
    )
    search_user_keys = (
        "collection_time",
        "uid",
        "sec_uid",
        "nickname",
        "unique_id",
        "short_id",
        "avatar",
        "signature",
        "verify",
        "enterprise",
        "follower_count",
        "total_favorited",
    )
    search_user_title = (
        "采集时间",
        "UID",
        "SEC_UID",
        "账号昵称",
        "抖音号",
        "SHORT_ID",
        "头像链接",
        "账号签名",
        "标签",
        "企业",
        "粉丝数量",
        "获赞数量",
    )
    search_user_type = (
        "TEXT",
        "TEXT",
        "TEXT",
        "TEXT",
        "TEXT",
        "TEXT",
        "TEXT",
        "TEXT",
        "TEXT",
        "TEXT",
        "INTEGER",
        "INTEGER",
    )
    search_live_keys = (
        "collection_time",
        "room_id",
        "uid",
        "sec_uid",
        "nickname",
        "short_id",
        "avatar",
        "signature",
        "verify",
        "enterprise",
    )
    search_live_title = (
        "采集时间",
        "直播ID",
        "UID",
        "SEC_UID",
        "账号昵称",
        "SHORT_ID",
        "头像链接",
        "账号签名",
        "标签",
        "企业",
    )
    search_live_type = (
        "TEXT",
        "TEXT",
        "TEXT",
        "TEXT",
        "TEXT",
        "TEXT",
        "TEXT",
        "TEXT",
        "TEXT",
        "TEXT",
    )
    hot_keys = (
        "position",
        "word",
        "hot_value",
        "cover",
        "event_time",
        "view_count",
        "video_count",
        "sentence_id",
    )
    hot_title = (
        "排名",
        "内容",
        "热度",
        "封面",
        "时间",
        "浏览数量",
        "视频数量",
        "SENTENCE_ID",
    )
    hot_type = (
        "INTEGER",
        "TEXT",
        "INTEGER",
        "TEXT",
        "TEXT",
        "INTEGER",
        "INTEGER",
        "TEXT",
    )
    LoggerParams = {
        "works": {
            "db_name": "WorksData.db",
            "title_line": works_text,
            "title_type": works_type,
            "field_keys": works_keys,
        },
        "comment": {
            "db_name": "CommentData.db",
            "title_line": comment_title,
            "title_type": comment_type,
            "field_keys": comment_keys,
        },
        "user": {
            "db_name": "UserData.db",
            "title_line": user_title,
            "title_type": user_type,
            "field_keys": user_keys,
        },
        "mix": {
            "db_name": "MixData.db",
            "title_line": works_text,
            "title_type": works_type,
            "field_keys": works_keys,
        },
        "search_general": {
            "db_name": "SearchData.db",
            "title_line": works_text,
            "title_type": works_type,
            "field_keys": works_keys,
        },
        "search_user": {
            "db_name": "SearchData.db",
            "title_line": search_user_title,
            "title_type": search_user_type,
            "field_keys": search_user_keys,
        },
        "search_live": {
            "db_name": "SearchData.db",
            "title_line": search_live_title,
            "title_type": search_live_type,
            "field_keys": search_live_keys,
        },
        "hot": {
            "db_name": "BoardData.db",
            "title_line": hot_title,
            "title_type": hot_type,
            "field_keys": hot_keys,
        },
    }
    DataLogger = {
        "csv": CSVLogger,
        "xlsx": XLSXLogger,
        "sql": SQLLogger,
        # "mysql": BaseTextLogger,
    }

    def run(
            self,
            parameter: "Parameter",
            folder="",
            type_="works",
            blank=False, ):
        root = parameter.root.joinpath(
            parameter.cleaner.filter_name(folder, False, "Data"))
        root.mkdir(exist_ok=True)
        params = self.LoggerParams[type_]
        logger = BaseTextLogger if blank else self.DataLogger.get(
            parameter.storage_format, BaseTextLogger)
        return root, params, logger
